package com.epfresh.adapter;

import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.epfresh.R;
import com.epfresh.api.universalimageloader.core.DisplayImageOptions;
import com.epfresh.api.universalimageloader.core.ImageLoader;
import com.epfresh.api.universalimageloader.core.assist.ImageScaleType;
import com.epfresh.api.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.epfresh.api.utils.LocalDisplay;
import com.epfresh.bean.ListStore;
import com.epfresh.bean.SecondKillTime;
import com.epfresh.views.FlexibleRoundedBitmapDisplayer;

import java.util.ArrayList;
import java.util.List;

import multitype.OnRecyclerViewItemClickListener;

public class StoreRecyclerListAdapter extends RecyclerView.Adapter<StoreRecyclerListAdapter.ViewHolder> implements View.OnClickListener {

    private List<ArrayList<ListStore>> list;

    OnRecyclerViewItemClickListener onRecyclerViewItemClickListener;

    public OnRecyclerViewItemClickListener getOnRecyclerViewItemClickListener() {
        return onRecyclerViewItemClickListener;
    }

    public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
        this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
    }

    public void setCategory( ArrayList<ArrayList<ListStore>> list) {
        this.list = list;
    }


    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.item_list_newstore_copy, parent, false);
        view.setOnClickListener(this);
        return new ViewHolder(view);
    }


    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        ViewGroup.LayoutParams lp = holder.itemView.getLayoutParams();
        ArrayList<ListStore> flag = list.get(position);

        for(int i=0;i<holder.storesNumber;i++){
             holder.stores[i].setVisibility(View.INVISIBLE);
        }
        for (int i = 0; i < flag.size()&&i< holder.storesNumber; i++) {
            ListStore storesBean= flag.get(i);

             holder.stores[i].setTag(R.id.item_key_position, 10*position+i);
             holder.iv_phone[i].setTag(R.id.item_key_position, 10*position+i);

             holder.stores[i].setVisibility(View.VISIBLE);
             holder.tv_store_name[i].setText(storesBean.getName());
            if(storesBean.getStall()!=null) {
                 holder.tv_store_number[i].setText(storesBean.getStall());
            }else{
                 holder.tv_store_number[i].setText("");
            }
             holder.tv_goods_number[i].setText("共"+storesBean.getOnShelvesProductCount()+"件商品");
             holder.iv_flag[i].setVisibility(View.GONE);
            //ImageLoader.getInstance().displayImage(storesBean.getLogoUrl(), new ImageViewAware( holder.iv_store[i]));
            setRoundedImage(storesBean.getHeaderImage(), 4, FlexibleRoundedBitmapDisplayer.CORNER_TOP_LEFT|FlexibleRoundedBitmapDisplayer.CORNER_TOP_RIGHT,
                    R.mipmap.store_head_defaut,
                     holder.iv_store[i]
            );
            if(storesBean.getIsAcceptOrders()!=null&&storesBean.getIsAcceptOrders()==1){
                if(storesBean.getRecType()!=null&&storesBean.getRecType()==1){
                     holder.iv_flag[i].setVisibility(View.VISIBLE);
                     holder.iv_flag[i].setImageResource(R.mipmap.store_recent_triangle);
                }else if(storesBean.getRecType()!=null&&storesBean.getRecType()==2) {
                     holder.iv_flag[i].setVisibility(View.VISIBLE);
                     holder.iv_flag[i].setImageResource(R.mipmap.store_concern_triangle);
                }
            }else{
                 holder.iv_flag[i].setVisibility(View.VISIBLE);
                 holder.iv_flag[i].setImageResource(R.mipmap.store_sleep_triangle);
            }
        }
    }


    @Override public int getItemCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public void onClick(View v) {
/*
        Object tag = v.getTag(R.id.item_key_position);
        if (tag != null && tag instanceof Integer){
            int position = (int) tag;
            list.get(position).setSelectNumber(position);
            notifyDataSetChanged();
            if (onRecyclerViewItemClickListener != null && position < list.size()){
                onRecyclerViewItemClickListener.onItemClick(v,position, list.get(position));
            }
        }*/
    }


    public static class ViewHolder extends RecyclerView.ViewHolder {

        int storesNumber = 2;
        View ll_store1;//, ll_store2;
        View[] stores = new View[storesNumber];//v_1, v_2, v_3, v_4, v_5, v_6;
        TextView[] tv_store_name = new TextView[storesNumber];
        TextView[] tv_store_number = new TextView[storesNumber];
        TextView[] tv_goods_number = new TextView[storesNumber];
        ImageView[] iv_flag = new ImageView[storesNumber];
        ImageView[] iv_store = new ImageView[storesNumber];
        View[] iv_phone = new ImageView[storesNumber];

        ViewHolder(final View convertView) {
            super(convertView);
            ll_store1 = convertView.findViewById(R.id.ll_store1);
            stores[0] = convertView.findViewById(R.id.v_1);
            stores[1] = convertView.findViewById(R.id.v_2);

            for(int i=0;i<storesNumber;i++){
                tv_store_name[i] = (TextView) stores[i].findViewById(R.id.tv_store_name);
                tv_store_number[i] = (TextView) stores[i].findViewById(R.id.tv_store_number);
                tv_goods_number[i] = (TextView) stores[i].findViewById(R.id.tv_goods_number);
                iv_phone[i] = stores[i].findViewById(R.id.iv_phone);
                iv_flag[i] = (ImageView) stores[i].findViewById(R.id.iv_flag);
                iv_store[i] = (ImageView) stores[i].findViewById(R.id.iv_store);
            }
        }
    }

    int w;
    public void notifyDataChanged(){
        if (list != null && list.size() > 0){
            int size = list.size();
            w = LocalDisplay.SCREEN_WIDTH_PIXELS;

            if(size == 1){
                w = LocalDisplay.SCREEN_WIDTH_PIXELS;
            }else if (size == 2){
                w /= 2;
            }else {
                w /= 3;
            }
        }

        notifyDataSetChanged();
    }

    public void setRoundedImage(String url, int cornerRadius, int corners, int image, ImageView imageView) {
        ImageLoader imageLoader = ImageLoader.getInstance();
        DisplayImageOptions options = new DisplayImageOptions.Builder()
                .showImageOnLoading(image).showStubImage(image)
                .showImageForEmptyUri(image)//url为空时显示的图片
                .showImageOnFail(image)//加载失败显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .displayer(new FlexibleRoundedBitmapDisplayer(cornerRadius,corners)) // 自定义增强型BitmapDisplayer
                .build();
        imageLoader.displayImage(url, imageView, options);
    }

    public static DisplayImageOptions getTopRoundOptions(String expectTag) {
        return new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.store_logo) // 设置图片在下载期间显示的图片
                .showImageForEmptyUri(R.mipmap.store_logo)// 设置图片Uri为空或是错误的时候显示的图片
                .showImageOnFail(R.mipmap.store_logo) // 设置图片加载/解码过程中错误时候显示的图片
                .cacheInMemory(true)// 设置下载的图片是否缓存在内存中
                .cacheOnDisk(true)// 设置下载的图片是否缓存在SD卡中
                .considerExifParams(true) // 是否考虑JPEG图像EXIF参数（旋转，翻转）
                .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)// 设置图片以如何的编码方式显示
                .bitmapConfig(Bitmap.Config.RGB_565)// 设置图片的解码类型//
                .resetViewBeforeLoading(true)// 设置图片在下载前是否重置，复位
                .displayer(new FadeInBitmapDisplayer(300, true, false, false))// 是否图片加载好后渐入的动画时间
                .setExceptTag(expectTag)
                .build();// 构建完成
    }

}
